package com.huaxin.hxcloud.activity;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v7.widget.Toolbar;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.huaxin.common.BaseActivity;
import com.huaxin.hxcloud.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ExpertSupportActivity extends BaseActivity implements
        OnBackStackChangedListener {

    public final static int QUESTIONLIST = 0x001;
    public final static int ADDQUESTION = QUESTIONLIST + 1;
    public final static int ADDEXTRA = QUESTIONLIST + 2;
    public final static int ANSWER = QUESTIONLIST + 3;
    public final static int CHECK = QUESTIONLIST + 4;
    public final static int ADOPT = QUESTIONLIST + 5;
//	public final static int WATCH_DETAIL = QUESTIONLIST + 6;
//	public final static int MYQUESTION = QUESTIONLIST+7;


    public Fragment current;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.more)
    ImageView more;
    @BindView(R.id.title_model)
    RelativeLayout titleModel;
    @BindView(R.id.content)
    FrameLayout content;


    @Override
    protected int getContentViewId() {
        return R.layout.activity_expert_support;
    }

    @Override
    public void initView() {
        toolbar.setTitle("问题浏览");
        switchContent(QUESTIONLIST);
        getSupportFragmentManager().addOnBackStackChangedListener(this);
    }



    public void switchContent(int fId, Parcelable extra) {
        Fragment f = null;
        switch (fId) {

            default:
                break;
        }
        if (f != null) {
            if (extra != null) {
                final Bundle bundle = new Bundle();
                bundle.putParcelable("extra", extra);
                f.setArguments(bundle);
            }
            switchContent(f);
        }
    }


    public void switchContent(int fId) {
        switchContent(fId, null);
    }

    public void switchContent(final Fragment f) {

        getSupportFragmentManager()
                .beginTransaction()

                .replace(R.id.content, f, f.getClass().getName())
                .addToBackStack(null).commit();
        System.out.println("switchContent:"
                + getFragmentManager().getBackStackEntryCount());
        current = f;
    }



    @Override
    public void onBackPressed() {
        current = getVisibleFragment();

        super.onBackPressed();
        current = getVisibleFragment();
    }

    public Fragment getVisibleFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        List<Fragment> fragments = fragmentManager.getFragments();
        if (fragments == null) {
            return null;
        }
        for (Fragment fragment : fragments) {
            if (fragment != null && fragment.isVisible())
                return fragment;
        }
        return null;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    public void onBackStackChanged() {
    }

}
